<?php
require_once 'protoweb/medidas_protoweb.php';

/**
 * @package sisco
 * @subpackage controller
 */
class Medidas extends Medidas_protoweb
{
    public function create()
    {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // Get an empty object
        $data['medida'] = new Medida();

        // Message Handler
        $data['msg'] = new PwMsgHandler();

        // Seleccionar secciones
        $data['navbar_productos'] = 'active';
        $data['sidebar_medidas'] = 'active';
        
        // prepare object attributes
        $this->createPrepare($data);

        // if posting, try to save the new object
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $this->create_save($data);
        }

        // load view
        $this->smartyview->render('medida_create.html', $data);

    } // function create

    private function create_save($data) 
    {
        // assignCreatePost is defined on parent class and 
        // will assign all posting values to your attributes's class 
        // based on purposes definitions
        if ($this->assignCreatePost($data)) {

            // You could customize attributes here

            // Saving Object (feel free to edit)
            if ($this->saveEntity($data)) { 
            
                // post processing object attributes
                $this->afterCreateSuccess($data);

                // Show success message in next view
                $data['msg']->success = 'medida successfully created';
                $data['msg']->persistSuccess();

                // redirects to update method
                redirect(base_url() . 'medidas/update/' . $data['medida']->id . '/');
            }
            else { 
                
                // revert attributes changes if necesary
                $this->revertOnCreateError($data);

                // Append model's errors
                $data['msg']->error .= $data['medida']->error->string;
            }
        } // if assignCreatePost
    } // function create_save
    
    public function update($id)
    {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // get object to edit
        $data['medida'] = new Medida($id);

        // Message Handler
        $data['msg'] = new PwMsgHandler();

        // Seleccionar secciones
        $data['navbar_productos'] = 'active';
        $data['sidebar_medidas'] = 'active';
        
        // prepare object attributes
        $this->updatePrepare($data);

        // if posting, try to save modifications
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            $this->update_save($data);
        }

        // load view
        $this->smartyview->render('medida_update.html', $data);

    } // function update

    private function update_save($data) 
    {
        // assignUpdatePost is defined on parent class and 
        // will assign all posting values to your attributes's class 
        // based on purposes definitions
        if ($this->assignUpdatePost($data)) {

            // You could customize attributes here

            // Saving Object and its relationships (feel free to edit)
            if ($this->saveEntity($data)) { 
            
                // post processing object attributes
                $this->afterUpdateSuccess($data);

                // Show success message in next view
                $data['msg']->success = 'medida successfully updated';
                $data['msg']->persistSuccess();

                // redirects to edit
                redirect(base_url() . 'medidas/update/' . $data['medida']->id . '/');
            }
            else { 
                // revert attributes changes if necesary
                $this->revertOnUpdateError($data);

                // Append model's errors
                $data['msg']->error .= $data['medida']->error->string;
            }
        } // if assignUpdatePost
    } // function update_save
    
    public function delete($id)
    {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // get object to delete
        $data['medida'] = new Medida($id);
        
        if ($data['medida']->delete()) {

            // delete related actions based on purpose
            $this->afterDeleteSuccess($data);

            // Show success message in next view
            $data['msg']          = new PwMsgHandler();
            $data['msg']->success = 'medida successfully deleted';
            $data['msg']->persistSuccess();

            // redirects to create a new object
            redirect(base_url() . 'medidas/create/');
        }
    } // function delete

    public function browse()
    {
        // chequeo y obtengo el usuario loggeado
        $data['logueado'] = $this->getUsuarioLogueado();

        // valido permisos
        $gruposValidos = array();
        // ej. array('venta','logistica','contable','directivo');
        $this->validarPermisos($data['logueado'], $gruposValidos);

        // Message Handler
        $data['msg'] = new PwMsgHandler();

        // Seleccionar secciones
        $data['navbar_productos'] = 'active';
        $data['sidebar_medidas'] = 'active';
        
        // get object to delete
        $data['medidas'] = new Medida();
        $data['medidas']->get()->all;
        
        $this->smartyview->render('medida_browse.html',$data);
    } // function browse

    public function index()
    {
        $this->browse();
    }
    

	public function __construct() 
	{
		parent::__construct();
	}

} // class Medidas

/* End of file medidas.php */
/* Location: ./application/controllers/medidas.php */